<?php

/**
 * @filename source_module.php 2013-11-11 02:10:56x
 * @author Huming Xu <huming17@126.com>
 * @version 1.0.0
 * @copyright DZF (c) 2013, Huming Xu
 */
class ctrl_course_main extends course_main {

    public $info_array = array();
    public $page_array = array();
    public $tree_array = array();

    static function &instance() {
        static $object;
        if (empty($object)) {
            $object = new self();
        }
        return $object;
    }
    public function do_sample($cm_id = '') {
        global $_G;
        $cm_id = isset($_REQUEST['id']) ? $_REQUEST['id'] : '';
        $detail = $this->one_info('course_main', 'cm_id', $cm_id);
        //DEBUG 调试输出
        if ($_G['gp_api']) {
            $return['code'] = '0';
            $return['data'] = $detail;
            echo format_data($return, $_G['gp_api'], $_G['gp_mod'], $_G['gp_action'], $_G['gp_do']);
        }
    }
    /*
     * 	取出一条信息
     */

    public function do_detail($cm_id = '') {
        global $_G;   
        $detail = array();
        $data = array(
            'type' => array(), //三级分类目录
            'detail' => array(
            	'tc_flag'=>0,//2016-12-17 08:32:23 未登录状态返回0
                'brief' => '',
                'chapter' => array(),
                'spread' => array(),
                'content' => array()
            )
        );
        $cm_id = isset($_REQUEST['id']) ? $_REQUEST['id'] : '';
       // $so_id = isset($_REQUEST['so_id']) ? $_REQUEST['so_id'] : 0;
        if(empty($cm_id)){
            $cm_id = isset($_REQUEST['cm_id']) ? $_REQUEST['cm_id'] : '';
        }
        if($_G['user_id']&&$cm_id){
            $data['detail']['tc_flag'] = $this->has_buy($cm_id,$_G['user_id']);
        }
        $detail = $this->one_info('course_main', 'cm_id', $cm_id);
        //DEBUG 课程简介
        if($detail['cm_introduction']){
        	$data['detail']['brief'] = $detail['cm_introduction'];
        }else{
        	$data['detail']['brief'] = '';
        }
        //DEBUG 获取课程三级分类 开始
        /* if($so_id){
        	$course_cate = array($detail['cm_cc_id_2_1'], $detail['cm_cc_id_2_2'], $detail['cm_cc_id_2_3']);
        }else{
        	$course_cate = array($detail['cm_cc_id_1_1'], $detail['cm_cc_id_1_2'], $detail['cm_cc_id_1_3']);
        } */
        $course_cate = array($detail['cm_cc_id_1_1'], $detail['cm_cc_id_1_2'], $detail['cm_cc_id_1_3']);
        $page_condition = array(
            'page' => 1, //int 请求页面 页码
            'limit' => 3, //int 每页请求个数
            'perpage' => 3, //int 每页显示个数
            'wheresql' => " AND cc_id IN (" . dimplode($course_cate) . ") AND isdelete=0  ", //string //条件SQL语句 
            'orderby' => 'ORDER BY cc_level ASC', //string 排序规则
        );
        $page_result = $this->api('course_cate', $page_condition);
        foreach ($page_result['page_data'] AS $key => $value) {
            $tmp = array("key" => '', "label" => '');
            $tmp['key'] = $value['cc_id'];
            $tmp['label'] = $value['cc_name'];
            $data['type'][] = $tmp;
        }
        //DEBUG 获取所有课程课时与章节       
       /*  $page_condition = array(
            'page' => 1, //int 请求页面 页码
            'limit' => 100, //int 每页请求个数
            'perpage' => 100, //int 每页显示个数
            'wheresql' => " AND ch_id IN (" . dimplode($course_hour_ids) . ") AND isdelete=0 ", //string //条件SQL语句 
            'orderby' => 'ORDER BY chc_sort ASC', //string 排序规则
        );
        $page_result_course_hour_chapter = $this->api('course_hour_chapter', $page_condition);
        $course_hour_chapter_ids = array();
        $course_hour_chapter_list = array();
        foreach ($page_result_course_hour_chapter['page_data'] AS $key => $value) {
            $course_hour_chapter_ids[$value['cc_id_1']] = $value['cc_id_1'];
            $tmp = $course_hour_list[$value['ch_id']];
            $course_hour_chapter_list[$value['cc_id_1']][] = $tmp;
        }print_r($page_result_course_hour_chapter);die; */
        $page_condition = array(
            'page' => 1, //int 请求页面 页码
            'limit' => 100, //int 每页请求个数
            'perpage' => 100, //int 每页显示个数
            'wheresql' => " AND cm_id = ".$cm_id." AND isdelete=0 ", //string //条件SQL语句 
            'orderby' => 'ORDER BY cc_sort ASC', //string 排序规则
        );
        $page_result_course_chapter = $this->api('course_chapter', $page_condition);
        foreach ($page_result_course_chapter['page_data'] AS $key => $value) {
        	$course_chapter_ids[$value['cc_id']] = $value['cc_id'];        	        	
        } 
        
        $page_condition = array(
        		'page' => 1, //int 请求页面 页码
        		'limit' => 100, //int 每页请求个数
        		'perpage' => 100, //int 每页显示个数
        		'wheresql' => " AND cm_id = '" . $cm_id . "' AND isdelete=0 ", //string //条件SQL语句
        		'orderby' => 'ORDER BY ch_sort,ch_id', //string 排序规则
        );
        $page_result_course_hour = $this->api('course_hour', $page_condition);
        $course_hour_ids = array();
        $course_hour_list = array();
        foreach ($page_result_course_hour['page_data'] AS $key => $value) {
        	$course_hour_ids[$value['ch_id']] = $value['ch_id'];
        	$tmp = array(
        			'title' => $value['ch_name'],
        			'teacher' => ext::user_realname($value['ch_teacher_id']),
        			'startTime' => date('m-d H:i', $value['ch_start_time']),
        			'lastTime' => date('H:i:s', $value['ch_end_time']),
        			'ch_duration' =>$value['ch_duration'],
        			'id' => $value['ch_id']
        	);
        	$course_hour_list[$value['cc_id']][] = $tmp;
        }         
        $chapter = array();
        foreach ($page_result_course_chapter['page_data'] AS $key => $value) {
            $chapter_tmp = array(
                "title" => $value['cc_name'],
                "children" => $course_hour_list[$value['cc_id']],
            );
            $data['detail']['chapter'][] = $chapter_tmp;
        }
        //DEBUG 获取相关课程 开始
        $page_condition = array(
            'page' => 1, //int 请求页面 页码
            'limit' => 3, //int 每页请求个数
            'perpage' => 3, //int 每页显示个数
            'wheresql' => " AND cm_flag =1 AND isdelete=0 AND so_id = ".$detail['so_id'] , //string //条件SQL语句 
            'orderby' => '', //string 排序规则
        );
        $page_result = $this->api('course_main', $page_condition);       
        foreach ($page_result['page_data'] AS $key => $value) {
            $tmp = array("type" => '', "startTime" => '', "title" => '', "price" => '', "school" => '', "id" => '', "img" => '');
            $tmp['type'] = $value['cm_cate_id'];
            $tmp['startTime'] = date('Y-m-d', $value['cm_start_time']);
            $tmp['title'] = $value['cm_name'];
            $tmp['price'] = $value['cm_price'];
            $school_online = $this->one_info('school_online', 'so_id', $detail['so_id']);
            $tmp['school'] = $school_online['so_name'];
            $tmp['id'] = $value['cm_id'];
            //$tmp['img'] = $value['cm_img'];
            if($value['cm_img']&&$value['cm_img']!='http://webapi.jxzhjy.com/'){
            	$tmp['img'] = $value['cm_img'];
            }else{
            	$tmp['img'] = "http://webapi.jxzhjy.com/data/schoolimg/default_img.jpg";
            } 
            $data['detail']['spread'][] = $tmp;
        }
        //DEBUG 获取相关课程 结束
        //DEBUG 课程详情
        if($detail){
	        $data['detail']['content']['type'] = $detail['cm_cate_id'];
	        $data['detail']['content']['startTime'] = date('Y-m-d', $detail['cm_start_time']);
	        $data['detail']['content']['endTime'] = date('Y-m-d', $detail['cm_end_time']);
	        $data['detail']['content']['title'] = $detail['cm_name'];
	        $data['detail']['content']['price'] = $detail['cm_price'];
	        $school_online = $this->one_info('school_online', 'so_id', $detail['so_id']);
	        $data['detail']['content']['school'] = $school_online['so_name'];
	        $data['detail']['content']['id'] = $detail['cm_id'];
	       // $data['detail']['content']['img'] = $detail['cm_img'];
	        if($detail['cm_img']&&$detail['cm_img']!='http://webapi.jxzhjy.com/'){
	        	$data['detail']['content']['img'] = $detail['cm_img'];
	        }else{
	        	$data['detail']['content']['img'] = "http://webapi.jxzhjy.com/data/schoolimg/default_img.jpg";
	        } 
	        $data['detail']['content']['info'] = $detail['cm_brief'];
	        $data['detail']['content']['classTimes'] = $detail['cm_hour_num'];
	        $data['detail']['content']['buyTimes'] = $detail['cm_count'];
	        $data['detail']['content']['is_group'] = $detail['is_group'];
	        if($data['detail']['content']['is_group'] ==1){
	            $cm_result = $this->get_gour_detail($detail['cm_id']);
	            foreach ($cm_result AS $key => $value) {
	                $cm = array("type" => '', "startTime" => '', "title" => '', "price" => '', "school" => '', "id" => '', "img" => '');
	                $cm['type'] = $value['cm_cate_id'];
	                $cm['startTime'] = date('Y-m-d', $value['cm_start_time']);
	                $cm['title'] = $value['cm_name'];
	                $cm['price'] = $value['cm_price'];
	                $school_online = $this->one_info('school_online', 'so_id', $detail['so_id']);
	                $cm['school'] = $school_online['so_name'];
	                $cm['id'] = $value['cm_id'];
	                //$cm['img'] = $value['cm_img'];
	                if($value['cm_img']&&$value['cm_img']!='http://webapi.jxzhjy.com/'){
	                	$cm['img'] = $value['cm_img'];
	                }else{
	                	$cm['img'] = "http://webapi.jxzhjy.com/data/schoolimg/default_img.jpg";
	                } 
	                $cm['is_group'] = $value['is_group'];
	                $data['detail']['content']['child_course'][] = $cm;
	            }
	        }else{
	        	$data['detail']['content']['child_course']=array();
	        }
        }else{
        	$data['detail']['content']=array();
        }
        //DEBUG 获取课程三级分类 结束
        //DEBUG 调试输出
        $return['code'] = '0';
        $return['data'] = $data;
        echo format_data($return, $_G['gp_api'], $_G['gp_mod'], $_G['gp_action'], $_G['gp_do']);
        //include template('course/main/detail');
    }

    /*
     *  添加系统信息
     */

    public function do_add() {
        global $_G;
        $issubmit = isset($_REQUEST['issubmit']) && !empty($_REQUEST['issubmit']) ? $_REQUEST['issubmit'] : '';
        $course_main = isset($_REQUEST['course_main']) && !empty($_REQUEST['course_main']) ? $_REQUEST['course_main'] : '';
        if ('1' == $issubmit) {
            //TODO 后端字段校验 根据具体业务逻辑添加
            //DEBUG 判断是否数组字段,如果是数组转换为逗号间隔 字符串存贮
            foreach ($course_main AS $key => $value) {
                if (is_array($value) && !empty($value)) {
                    $course_main[$key] = implode(",", $value);
                }
            }
            $course_main['create_dateline'] = TIMESTAMP;
            $insert_id = $this->add('course_main', $course_main);
            if ($insert_id) {
                showmessage('操作成功', 'index.php?mod=course&action=main&do=index');
            } else {
                showmessage('操作失败', 'index.php?mod=course&action=main&do=index');
            }
        } else {
            include template('course/main/add');
        }
    }

    /*
     *  编辑系统信息
     */

    public function do_edit() {
        global $_G;
        $issubmit = isset($_REQUEST['issubmit']) && !empty($_REQUEST['issubmit']) ? $_REQUEST['issubmit'] : '';
        $course_main = isset($_REQUEST['course_main']) && !empty($_REQUEST['course_main']) ? $_REQUEST['course_main'] : '';
        $cm_id = isset($_REQUEST['cm_id']) ? $_REQUEST['cm_id'] : '';
        if ($cm_id) {
            if ('1' == $issubmit) {
                $course_main['modify_dateline'] = TIMESTAMP;
                //DEBUG 判断是否数组字段,如果是数组转换为逗号间隔 字符串存贮
                foreach ($course_main AS $key => $value) {
                    if (is_array($value) && !empty($value)) {
                        $course_main[$key] = implode(",", $value);
                    }
                }
                $where = array('cm_id' => $cm_id);
                $effect_row = $this->edit('course_main', $course_main, $where);
                if ($effect_row) {
                    showmessage('操作成功', 'index.php?mod=course&action=main&do=index');
                } else {
                    showmessage('操作失败', 'index.php?mod=course&action=main&do=index');
                }
            } else {
                //DEBUG 获取操作对象信息
                $info = $this->one_info('course_main', 'cm_id', $cm_id);
                include template('course/main/edit');
            }
        } else {
            showmessage('请选择编辑对象', 'index.php?mod=course&action=main&do=index');
        }
    }

    /*
     *  删除系统信息
     */

    public function do_delete() {
        global $_G;
        $cm_id = isset($_REQUEST['cm_id']) ? $_REQUEST['cm_id'] : '';
        if ($cm_id) {
            $effect_row = $this->delete("course_main", array("cm_id" => $cm_id), $limit = 1);
            if ($effect_row) {
                showmessage('操作成功', 'index.php?mod=course&action=main&do=index');
            } else {
                showmessage('操作失败', 'index.php?mod=course&action=main&do=index');
            }
        }
    }

    /*
     *  系统信息列表
     */

    public function do_index() {
        global $_G;
        $api = isset($_REQUEST['api']) ? $_REQUEST['api'] : 'json';
        $client = isset($_REQUEST['client']) ? $_REQUEST['client'] : 'wap';
        $page = max(1, intval($_REQUEST['page']));
        $default_limit = 20;
        $cm_recommend = isset($_REQUEST['cm_recommend']) ? $_REQUEST['cm_recommend'] : '';
        $guess_you_like = isset($_REQUEST['guess_you_like']) ? $_REQUEST['guess_you_like'] : '';
        $so_id = isset($_REQUEST['so_id']) ? $_REQUEST['so_id'] : 0;
        if ($cm_recommend) {
            $default_limit = 4;
        }
        if ($guess_you_like) {
            $default_limit = 6;
        }
        $perpage = $limit = isset($_REQUEST['limit']) && !empty($_REQUEST['limit']) ? $_REQUEST['limit'] : $default_limit;
        $start = (($page - 1) * $perpage);
        $wheresql = " AND isdelete =0 and cm_flag = 1 and so_id = ".$so_id;
        $keyword = isset($_REQUEST['keyword']) ? $_REQUEST['keyword'] : '';
        if ($keyword) {
            $wheresql .= " AND cm_name LIKE '%" . $keyword . "%' ";
        }
        $k = isset($_REQUEST['k']) ? $_REQUEST['k'] : '';
        $l = isset($_REQUEST['l']) ? $_REQUEST['l'] : '1';
        if ($k && $l) {
            $wheresql .= " AND cm_cc_id_1_" . $l . " = '" . $k . "' ";
        }
        if ($cm_recommend) {
            $wheresql .= " AND cm_recommend = '" . $cm_recommend . "' ";
        }
        $is_group = isset($_REQUEST['is_group']) ? $_REQUEST['is_group'] : '';
        if ($is_group !='') {
            $wheresql .= " AND is_group = '" . $is_group . "' ";
        }
        
        $cm_price = isset($_REQUEST['cm_price']) ? $_REQUEST['cm_price'] : '';
        if ($cm_price == 1) {
            $wheresql .= " AND cm_price = '0' ";
        }
        if ($cm_price == 2) {
            $wheresql .= " AND cm_price > '0' ";
        }
        $cm_sort_name = isset($_REQUEST['cm_sort_name']) ? $_REQUEST['cm_sort_name'] : '';
        $cm_sort_type = isset($_REQUEST['cm_sort_type']) ? $_REQUEST['cm_sort_type'] : '';
        $cm_sort_name_array = array("cm_id", "cm_price", "cm_count", "good_review", "cm_price");
        $cm_sort_type_array = array('DESC', 'ASC');
        if ($cm_sort_name && $cm_sort_type && in_array($cm_sort_type, $cm_sort_type_array) && in_array($cm_sort_name, $cm_sort_name_array)) {
            $orderby = " ORDER BY " . $cm_sort_name . " " . $cm_sort_type;
        } else {
            $orderby = " ORDER BY cm_id DESC ";
        }
        //DEBUG 初始化请求请求获取一页列表数据的参数
        $page_condition = array(
            'page' => $page, //int 请求页面 页码
            'limit' => $limit, //int 每页请求个数
            'perpage' => $perpage, //int 每页显示个数
            'wheresql' => $wheresql, //string //条件SQL语句 
            'orderby' => $orderby, //string 排序规则
        );
        $page_result = $this->index('course_main', $page_condition);
        if ($client == 'pc') {
            foreach ($page_result['page_data'] AS $key => $value) {
                $tmp_value['type'] = $value['cm_cate_id'];
                $tmp_value['startTime'] = date('Y-m-d', $value['cm_start_time']);
                $tmp_value['title'] = $value['cm_name'];
                $tmp_value['price'] = $value['cm_price'];
                $tmp_value['school'] = $this->get_field_value_by_id('school_online', 'so_id', $value['so_id'], 'so_name');
                ;
                $tmp_value['id'] = $value['cm_id'];
                if($value['cm_img']&&$value['cm_img']!='http://webapi.jxzhjy.com/'){
                	$tmp_value['img'] = $value['cm_img'];
                }else{
                	$tmp_value['img'] = "http://webapi.jxzhjy.com/data/schoolimg/default_img.jpg";
                }               
                $tmp_value['info'] = $value['cm_brief'];
                $tmp_value['is_group'] = $value['is_group'];
                $page_result['page_data'][$key] = $tmp_value;
            }
            //DEBUG 获取推荐课程/相关课程 开始
            if($k && $l){
            	$couse_where = " AND cm_flag =1 AND isdelete=0 AND cm_cc_id_1_" . $l . " ='" . $k . "' and so_id = ".$so_id;
            }else{
            	$couse_where = " AND cm_flag =1 AND isdelete=0  and so_id = ".$so_id;
            }
            $page_condition = array(
                'page' => 1, //int 请求页面 页码
                'limit' => 5, //int 每页请求个数
                'perpage' => 5, //int 每页显示个数
                'wheresql' => $couse_where, //string //条件SQL语句 
                'orderby' => ' order by cm_count DESC', //string 排序规则
            );
            $course_spread = $this->api('course_main', $page_condition);
            if($course_spread['page_data']){
	            foreach ($course_spread['page_data'] AS $key => $value) {
	                $tmp = array("type" => '', "startTime" => '', "title" => '', "price" => '', "school" => '', "id" => '', "img" => '');
	                $tmp['type'] = $value['cm_cate_id'];
	                $tmp['startTime'] = date('Y-m-d', $value['cm_start_time']);
	                $tmp['title'] = $value['cm_name'];
	                $tmp['price'] = $value['cm_price'];
	                $school_online = $this->one_info('school_online', 'so_id', $value['so_id']);
	                $tmp['school'] = $school_online['so_name'];
	                $tmp['id'] = $value['cm_id'];
	                if($value['cm_img']&&$value['cm_img']!='http://webapi.jxzhjy.com/'){
	                	$tmp['img'] = $value['cm_img'];
	                }else{
	                	$tmp['img'] = "http://webapi.jxzhjy.com/data/schoolimg/default_img.jpg";
	                } 
	                $tmp['is_group'] = $value['is_group'];
	                $page_result['spread'][] = $tmp;
	            }
            }else{
            	$page_result['spread']=array();
            }
            //DEBUG DEBUG 获取推荐课程/相关课程 结束
        } else {
            foreach ($page_result['page_data'] AS $key => $value) {
                $tmp_value['cm_id'] = $value['cm_id'];
                $tmp_value['cm_name'] = $value['cm_name'];
                $tmp_value['cm_brief'] = $value['cm_brief'];
                if($value['cm_img']&&$value['cm_img']!='http://webapi.jxzhjy.com/'){
                	$tmp_value['img'] = $value['cm_img'];
                }else{
                	$tmp_value['img'] = "http://webapi.jxzhjy.com/data/schoolimg/default_img.jpg";
                }
                //$tmp_value['cm_img'] = $value['cm_img'];
                $tmp_value['cm_price'] = $value['cm_price'];
                $tmp_value['cm_count'] = $value['cm_count'];
                $tmp_value['so_id'] = $value['so_id'];
                $tmp_value['so_name'] = $this->get_field_value_by_id('school_online', 'so_id', $value['so_id'], 'so_name');
                $page_result['page_data'][$key] = $tmp_value;
            }
        }
        if ($_G['gp_api']) {
            $return['code'] = '0';
            if ($client == 'pc') {
                $return['data'] = $page_result;
            } else {
                $return['data']['course_main_index'] = $page_result;
            }
            echo format_data($return, $_G['gp_api'], $_G['gp_mod'], $_G['gp_action'], $_G['gp_do']);
        } else {
            //$multipage = multi($page_result['total_rows'], $perpage, $page, "index.php?mod=course&action=main&do=index&keyword=".$keyword);
            //include template('course/main/index');	  
        }
    }

    /**
     * 接口 AJAX或外部请求检索数据
     *
     * @param $return_type string json/xml.
     *
     * @return print string json/xml.
     */
    public function do_api() {
        $return = '';
        $page = max(1, intval($_GET['page']));
        $perpage = $limit = 20;
        $start = (($page - 1) * $perpage);
        $keyword = isset($_REQUEST['keyword']) ? $_REQUEST['keyword'] : '';
        $return_type = isset($_REQUEST['return_type']) ? $_REQUEST['return_type'] : 'json';
        $wheresql = "";
        if ($keyword) {
            $wheresql = " WHERE title LIKE '%" . $keyword . "%'";
        }
        $orderby = " ORDER BY cm_id DESC ";
        //DEBUG 初始化请求请求获取一页列表数据的参数
        $page_condition = array(
            'page' => $page, //int 请求页面 页码
            'limit' => $limit, //int 每页请求个数
            'perpage' => $perpage, //int 每页显示个数
            'wheresql' => $wheresql, //string //条件SQL语句 
            'orderby' => $orderby, //string 排序规则
        );
        //DEBUG 列表数据返回结构
        /*
          $page_result = array(
          //int 返回结果总数
          'total_rows' => $total_rows,
          //array 一页数据数组
          'page_data' => $page_data
          );
         */
        $page_result = $this->api('course_main', $page_condition);
        if ('json' == $return_type) {
            $return = json_encode($page_result);
        } elseif ('xml' == $return_type) {
            $return = array2xml($page_result);
        }
        //DEBUG 接口响应直接输出返回
        echo $return;
    }

}

?>